﻿using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Web;

using Joyintech.Common;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;

namespace Joyintech.Admin.inpiao365.general
{
    public partial class menu : System.Web.UI.Page
    {
        private ArrayList arr = new ArrayList();

        private int statusType = 0;

        private StringBuilder str = new StringBuilder();

        #region 窗体加载

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //获取权限信息
                arr = (ArrayList)systemUser_BLL.GetInstance().GetObject(HttpContext.Current.Session.SessionID + "_SESSION_USERFUN");

                if (DNTRequest.GetQueryString("menu_id") != null && PageValidate.IsNumberSign(DNTRequest.GetQueryString("menu_id").ToString()))
                {
                    string menu_id = DNTRequest.GetQueryString("menu_id").ToString();

                    str.Append("<table width='100%' height='100%' border='0' cellspacing='0' cellpadding='0' class='outFullLine'>");

                    DataBindMenu(menu_id);

                    str.Append("<tr><td>&nbsp;</td></tr></table>");

                    this.litMenu.Text = str.ToString();
                }
            }
        }

        #endregion


        #region 获取菜单的所有子菜单

        private bool DataBindMenu(string menu_id)
        {
            bool haveChild = false;

            DataSet ds = new DataSet();

            ds = new systemMenu_BLL().GetList("MenuPID=" + menu_id + " order by MenuOrder");

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                haveChild = true;

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (arr.IndexOf(dr["MenuID"].ToString()) >= 0)
                    {
                        if (CheckChildMenu(dr["MenuID"].ToString()))
                        {
                            str.Append(string.Format("<tr id='statusType{0}'><td class='btnSubModule' onClick='OpenCloseSubMenu({1})' id='menuTitleBox{2}'><div id='menuTitle{3}'><table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td nowrap title='{4}'>{5}</td><td align='right' width='16'><span class='btnSubMenuOpen'>&nbsp;</span></td></tr></table></div></td></tr>", statusType, statusType, statusType, statusType, dr["MenuName"].ToString(), dr["MenuName"].ToString()));

                            //获取子菜单
                            BindChildMenu(dr["MenuID"].ToString());
                        }
                        else
                        {
                            str.Append(string.Format("<tr><td class='btnSubModule' id='menuTitleBox{0}' onclick=\"OpenCloseSubMenu({1});menuJump('{2}');\" title='{3}'><div id='menuTitle{4}'>{5}</div></td></tr>", statusType, statusType, dr["menuUrl"].ToString(), dr["MenuName"].ToString(), statusType, dr["MenuName"].ToString()));
                        }

                        statusType++;
                    }
                }
            }

            return haveChild;
        }

        /// <summary>
        /// 绑定子菜单
        /// </summary>
        /// <param name="menu_id"></param>
        /// <param name="Nds"></param>
        /// <returns></returns>
        private bool BindChildMenu(string menu_id)
        {
            bool haveChild = false;

            DataSet ds = new DataSet();

            ds = new systemMenu_BLL().GetList("MenuPID=" + menu_id + " ORDER BY MenuOrder");

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                haveChild = true;

                str.Append(string.Format("<tr id='submenuBox{0}' valign='top' style='display:none;'><td class='btnSubModuleBox'>", statusType));

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (arr.IndexOf(dr["menuId"].ToString()) >= 0)
                    {
                        str.Append(string.Format("<div style='height:20px'><img src='/theme/images/square_blueS.gif' align='absMiddle'> <a href='{0}' target='main'>{1}</a><br /></div>", dr["menuUrl"].ToString(), dr["MenuName"].ToString()));

                        //statusType++;
                    }
                }

                str.Append("</td></tr>");
            }

            return haveChild;
        }

        /// <summary>
        /// 检查有没有子菜单
        /// </summary>
        /// <param name="menu_id"></param>
        /// <returns></returns>
        private bool CheckChildMenu(string menu_id)
        {
            bool haveChild = false;

            DataSet ds = new DataSet();

            ds = new systemMenu_BLL().GetList("MenuPID=" + menu_id + " ORDER BY MenuOrder");

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (arr.IndexOf(dr["menuId"].ToString()) >= 0)
                    {
                        haveChild = true;
                        break;
                    }
                }
            }

            return haveChild;
        }

        #endregion
    }
}